﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace MapTool
{
    [Serializable]
    public class MapWafer : INotifyPropertyChanged
    {

        /// <summary>
        /// ID
        /// </summary>
        private int _id = 0;
        public int ID
        {
            get => _id;
            set
            {
                _id = value;
                OnPropertyChanging();
            }
        }

        /// <summary>
        /// Name
        /// </summary>
        private string _name = "";
        public string Name
        {
            get => _name;
            set
            {
                _name = value;
                OnPropertyChanging();
            }
        }

        /// <summary>
        /// MapFilePath
        /// </summary>
        private string _mapFilePath= "";
        public string MapFilePath
        {
            get => _mapFilePath;
            set
            {
                _mapFilePath = value;
                OnPropertyChanging();
            }
        }

        public List<string> MapReadString  { get; set; }=new List<string>();

        /// <summary>
        /// X向Die的数量
        /// </summary>
        private int dieCount_X = 1;
        public int DieCount_X
        {
            get
            {
               return  dieCount_X;
            }
            set { dieCount_X = value; OnPropertyChanging(); }
        }
        /// <summary>
        /// Y向Die的数量
        /// </summary>
        private int dieCount_Y = 1;
        public int DieCount_Y
        {
            get { return dieCount_Y; }
            set { dieCount_Y = value; OnPropertyChanging(); }
        }
        /// <summary>
        /// Die的宽度
        /// </summary>
        private double dieWidth = 1;
        public double DieWidth
        {
            get { return dieWidth; }
            set { dieWidth = value; OnPropertyChanging(); }
        }
        /// <summary>
        /// Die的高度
        /// </summary>
        private double dieHeight = 1;
        public double DieHeight
        {
            get { return dieHeight; }
            set { dieHeight = value; OnPropertyChanging(); }
        }
        /// <summary>
        /// 晶圆切割道宽度
        /// </summary>
        private double cutRoadWidth = 1;
        public double CutRoadWidth
        {
            get { return cutRoadWidth; }
            set { cutRoadWidth = value; OnPropertyChanging(); }
        }
        /// <summary>
        /// 晶圆切割道高度
        /// </summary>
        private int cutRoadHeight = 1;
        public int CutRoadHeight
        {
            get { return cutRoadHeight; }
            set { cutRoadHeight = value; OnPropertyChanging(); }
        }
        public int DieWidthRatioCutRoadWidth
        {
            get { return (int)(1); }
        }
        public int DieHeightRatioCutRoadHeight
        {
            get { return (int)(1); }
        }
        public int UnitPix_X
        {
            get
            {
                if (DieCount_X > 0)
                {
                    return (int)(4000 / DieCount_X);
                }
                else
                {
                    return 0;
                }
            }
        }
        public int UnitPix_Y
        {
            get
            {
                if (DieCount_Y > 0)
                {
                    return (int)(4000 / DieCount_Y);
                }
                else { return 0; }
            }
        }
        public int OneDiePix_X
        {
            get { return (int)(UnitPix_X-1); }
        }
        public int OneDiePix_Y
        {
            get { return (int)(UnitPix_Y-1); }
        }
        public int OneCutRoadPix_X
        {
            get { return 1; }
        }
        public int OneCutRoadPix_Y
        {
            get { return 1; }
        }
       
        /// <summary>
        /// 晶圆Mark点的DieX
        /// </summary>
        public int MarkDieX { get; set; }
        /// <summary>
        /// 晶圆Mark点的DieY
        /// </summary>
        public int MarkDieY { get; set; }

        /// <summary>
        /// 获取图片宽度
        /// </summary>
        public int ImageWidth { get; set; } = 4000;
        /// <summary>
        /// 获取图片高度
        /// </summary>
        public int ImageHeight { get; set; } = 4000;
        /// <summary>
        /// 晶圆直径
        /// </summary>
        public double WaferDiameter { get; set; } = 30.5 * 60 * 12;



        /// <summary>
        /// 图像数据
        /// </summary>
   
        [JsonIgnore]
        public byte[] Pixels { get; set; }


        private List<PointId>? _diePoints;
        public List<PointId>? DiePoints
        {
            get { return _diePoints; }
            set
            {
                _diePoints = value;
                OnPropertyChanging();
            }
        }


        private PointId[,] _xyPoints;
        public PointId[,] XYPoints
        {
            get { return _xyPoints; }
            set
            {
                _xyPoints = value;
                OnPropertyChanging();
            }
        }



        public int DieOffsetCount_X { get;  set; }
        public int DieOffsetCount_Y { get;  set; }


        private ObservableCollection<string>? _rulekeysItem { get; set; } = new ObservableCollection<string>();
        public ObservableCollection<string>? RulekeysItem
        {
            get { return _rulekeysItem; }
            set
            {
                _rulekeysItem = value;
                OnPropertyChanging();
            }
        }


        private ObservableCollection<Rules>? _rules { get; set; } = new ObservableCollection<Rules>();
        public ObservableCollection<Rules>? Rules
        {
            get { return _rules; }
            set
            {
                _rules = value;
                OnPropertyChanging();
            }
        }

        private ObservableCollection<MergingRule>? _mergingRules { get; set; } = new ObservableCollection<MergingRule>();
        public ObservableCollection<MergingRule>? MergingRules
        {
            get { return _mergingRules; }
            set
            {
                _mergingRules = value;
                OnPropertyChanging();
            }
        }


        public SeColor CutRoadColor = new() { A = 0, R = 0, G = 0, B = 0 };


        /// <summary>
        /// 扫雷的边界
        /// </summary>
        private int areaMineSweepSize = 3;
        public int AreaMineSweepSize
        {
            get { return areaMineSweepSize; }
            set { areaMineSweepSize = value; OnPropertyChanging(); }
        }

        /// <summary>
        /// 扫雷允许NG数量
        /// </summary>
        private int areaMineSweepAllowCount = 3;
        public int AreaMineSweepAllowCount
        {
            get { return areaMineSweepAllowCount; }
            set { areaMineSweepAllowCount = value; OnPropertyChanging(); }
        }

        /// <summary>
        /// 扫雷Key
        /// </summary>
        private string areaMineSweepKey= "";
        public string AreaMineSweepKey
        {
            get { return areaMineSweepKey; }
            set { areaMineSweepKey = value; OnPropertyChanging(); }
        }


        public event PropertyChangedEventHandler? PropertyChanged;
        public void OnPropertyChanging([CallerMemberName] string? propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}